//*********基础功能重新封装**************//
function apiGet( url , data , func){
    log("进入apiGet,正在获取数据")
    logJson(data)


    api.ajax({
        url: baseApi + url,
        method: 'GET',
        returnAll:true,
        headers: {
            cookie: cookie()
        },
        dataType:"json",
        data:  data
    }, function(ret, err) {
        var cookie = getCookie(ret.headers)
        if(typeof cookie == "string"){
            setLocData("cookie",cookie);
        }
        try{
            // var res = JSON.parse(ret.body)
            // var res = JSON.parse(data)
            var res = ret.body
                logJson(res);
        }catch(e){
            showToast("数据异常");
            return;
        }finally{
        }
        apiDealWithResult(res,func)
    });
    
}


function apiPost( url , data ,func){
    log("进入apiGet,正在获取数据")
    log(url)
    logJson(data)


    api.ajax({
        url: baseApi + url,
        method: 'POST',
        returnAll:true,
        headers: {
            cookie: cookie()
        },
        dataType:"json",
        data:  data
    }, function(ret, err) {
        var cookie = getCookie(ret.headers)
        if(typeof cookie == "string"){
            setLocData("cookie",cookie);
        }
        try{
            // var res = JSON.parse(ret.body)
            // var res = JSON.parse(data)
            var res = ret.body
            logJson(res);
        }catch(e){
            showToast("数据异常");
            return;
        }finally{
        }
        apiDealWithResult(res,func)
    });
}


function uploadOnePic (data ,func) {
    log("uploadOnePic")
    if (toOSS == 1 ) {
        uploadOnePicToOss(data,func)
    }else{
        var param = {Filedata:{path:data}}
        apiPost( "/MyWebPicUpload" , param ,func)
    }


}

function getCookie(headers){

  var cookies = [];
  for (var i in headers) {
    if (i == "Set-Cookie") {
        var arr = headers[i].split(";")
        cookies = arr[0];
    }
  }
    log("这是获取到的cookie");
  logJson(cookies);
  return cookies;
}

function cookie(){
  var cookie = getLocData("cookie");
  return cookie;
}


//********表单数据提交*************

//获取表单数据
function getFormData(selector, name){
    return $(selector).find("[name='" + name + "']").val();
}
//重新设置处理过的表单数据
function setFormData(selector, name , value){
    $(selector).find("[name='" + name + "']").val(value)
}
//提交表单
function submit (selector , url ,callback){
    //取出表单信息
    var formDataArray = $(selector).serializeArray();
    var params = {};
    formDataArray.map(function (data) {
        if(data.name.substr(-2) == "[]"){
            var name = data.name.slice(0,-1);
            name += Math.random() + "]";
            data.name = name;
        }
        if(data.name == "password"){
            params[data.name] = MD5(data.value);
        }else{
            params[data.name] = data.value;
        }
    })
    
    appcan.ajax({
        url :  baseApi + url,
        type : 'POST',
        data : params,
        headers: {
          cookie: cookie()
        },
        success : function(data, status,requestCode,response, xhr) {
            logJson(response);
            var cookie = getCookie(response)
            if(typeof cookie == "string"){
                setLocData("cookie",cookie);
            }
            try{
                var res = JSON.parse(data)
            }catch(e){
                showToast("数据异常");
                return;
            }finally{
            }
            apiDealWithResult(res,callback)
        },
        error : function(e) {
            showToast("网络请求发生错误");
        }
    }); 
    
    return false;
}

function apiDealWithResult(res,callback) {
    log("数据获取成功");
    log(res);
    if(res.s == 0){
        //用户级错误
        if(res.d == "1"){
            showConfirm(
                res.i,
                function () {
                    //confirm
                },
                function () {
                    //cancel
                }
            )
        }else{
            showToast(res.i);
        }
    }
    if(res.s == 1){
        //正常数据
        callback(res)

    }
    if(res.s == 203){
        //未登录
        showToast(res.i);
        setTimeout(function () {
            goto("userLogin");
        },1000)
    }
    if(res.s == 301){
        //未登录
        showToast(res.i);
        update();
    }
    if(res.s == 404){
        //内容未找到
        showToast(res.i);
        goto("ERROR",{})
    }
    if(res.s == 500){
        //系统级错误
        showToast(res.i);
    }
}

function update () {
    // apiGet("/app/update",{key:'version',v:version,app:AppClientName},function (res) {
    //     if(res.d.update == 1 ){
    //         appcan.window.open("updateApp",res.d.url,0,0,0)
    //     }
    //   log(res)
    // })
}
